﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
   public class ExpressService
    {
       public int Edit(TB_Express entity)
       {
           SqlParameter[] sqlParameters = new SqlParameter[]
           {
               new SqlParameter("@Id",SqlDbType.BigInt),
               new SqlParameter("@Name",SqlDbType.VarChar,70), 
           };
           sqlParameters[0].Value = entity.Id;
           sqlParameters[1].Value = entity.ExpressName;
           string sql = entity.Id == 0
               ? "insert into TB_Express (ExpressName,del)values(@Name,0)"
               : "update TB_Express set ExpressName=@Name where id=@Id";
           return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, sqlParameters);
       }

       public DataSet GetList(int pageIndex, int pageCount, string name)
       {
           SqlParameter parameter = new SqlParameter("@name", SqlDbType.VarChar, 50);
           parameter.Value = name;
           string where = "";
           where = name == "" ? where : (where + " and ExpressName like  N'%'+@name+'%'");
           parameter.Value = name;
           string sql = "select count(*) as count from [dbo].[TB_Express]" + (where != "" ? " where " + (where.Substring(4, where.Length - 4)) : "") + "  select top(" + pageCount + ") * from [dbo].[TB_Express] where id not in (select top(" + pageIndex + ") Id from [dbo].[TB_Express] order by Id desc) " + where + " and del=0 order by id desc";
           DataSet set = SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql, parameter);
           set.Tables[0].TableName = "count";
           set.Tables[1].TableName = "data";
           return set;
       }

       public DataTable GetAllList()
       {
           string sql = "select * from TB_Express where del=0";
           return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
       }
    }
}
